package com.naishuo.bigevent.controller.user;

import com.naishuo.bigevent.pojo.Result;
import com.naishuo.bigevent.pojo.User;
import com.naishuo.bigevent.service.UserService;
import com.naishuo.bigevent.utils.JwtUtil;
import com.naishuo.bigevent.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private JwtUtil jwtUtil;

    // 注册
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^[a-zA-Z0-9_-]{3,16}$") String username,@Pattern(regexp = "^[a-zA-Z0-9_-]{3,16}$")  String password){
        if (userService.finfdByUsername(username) != null){
            return Result.error("用户名已存在");
        }
        userService.register(username,password);
        return Result.success("注册成功");
    }

    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^[a-zA-Z0-9_-]{3,16}$") String username,@Pattern(regexp = "^[a-zA-Z0-9_-]{3,16}$")  String password){
        User loginuser = userService.finfdByUsername(username);
        if (loginuser == null){
            return Result.error("用户名不存在");
        }
        password = com.naishuo.bigevent.utils.Md5Util.getMD5String(password);
        if (userService.finfdByUsername(username).getPassword().equals(password)){
            //登录成功
            Map<String,Object> map = new HashMap<>();
            map.put("id",loginuser.getId());
            map.put("username",loginuser.getUsername());
            String token = jwtUtil.generateToken(map);
            ValueOperations<String,String> valueOperations = stringRedisTemplate.opsForValue();
            valueOperations.set(token,token,1, TimeUnit.HOURS);
            return Result.success(token);
        }
        return Result.error("密码错误");
    }


    @GetMapping("/userinfo")
    public Result<User> userinfo(){


        Map<String,Object> map = ThreadLocalUtil.get();

        User user = userService.finfdByUsername(map.get("username").toString());

        return Result.success(user);

    }

    //更新用户数据
    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user){
        userService.update(user);
        return Result.success();
    }

    //更新用户头像
    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam @URL String avatarUrl){

        userService.updateAvatar(avatarUrl);
        return Result.success();
    }

    //更新用户密码
    @PatchMapping("/updatePassword")//@RequestParam String oldPassword,@RequestParam String newPassword,@RequestParam String confirmPassword不能用此接受参数
    public Result updatePassword(@RequestBody Map<String,String> params,@RequestHeader("Authorization") String token){
        String oldPassword = params.get("oldPassword");
        String newPassword = params.get("newPassword");
        String confirmPassword = params.get("confirmPassword");


        if (!newPassword.equals(confirmPassword)){
            return Result.error("两次密码不一致");
        }

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");

        if (!userService.finfdByUsername(map.get("username").toString()).getPassword().equals(com.naishuo.bigevent.utils.Md5Util.getMD5String(oldPassword))){
            return Result.error("旧密码错误");
        }
        userService.updatePassword(newPassword,id);
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();

        valueOperations.getOperations().delete(token);
        return Result.success();
    }




}